home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-04-25 | 14.1 KB | 448 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: DrwDDCmd.cpp
- // Release Version: $ ODF 1 $
- //
- // Author: Mary Boetcher
- //
- // Copyright: (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #include "ODFDraw.hpp"
-
- #ifndef DRWDDCMD_H
- #include "DrwDDCmd.h"
- #endif
-
- #ifndef DRAWCONT_H
- #include "DrawCont.h"
- #endif
-
- #ifndef DRAWSEL_H
- #include "DrawSel.h"
- #endif
-
- #ifndef BASESHP_H
- #include "BaseShp.h"
- #endif
-
- #ifndef DRAWPART_H
- #include "DrawPart.h"
- #endif
-
- #ifndef DRAWCLIP_H
- #include "DrawClip.h"
- #endif
-
- #ifndef DRAWLINK_H
- #include "DrawLink.h"
- #endif
-
- // ----- FrameWork Includes -----
-
- #ifndef FWFRAME_H
- #include "FWFrame.h"
- #endif
-
- #ifndef FWPRESEN_H
- #include "FWPresen.h"
- #endif
-
- //========================================================================================
- // RunTime Info
- //========================================================================================
-
- #ifdef FW_BUILD_MAC
- #pragma segment odfdrawcommand
- #endif
-
- FW_DEFINE_AUTO(CDrawDragCommand)
- FW_DEFINE_AUTO(CDrawDropCommand)
-
- //========================================================================================
- // class CDrawDragCommand
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // CDrawDragCommand constructor
- //----------------------------------------------------------------------------------------
- CDrawDragCommand::CDrawDragCommand(Environment* ev,
- CDrawPart* part,
- FW_CFrame* frame,
- CDrawSelection* selection,
- FW_Boolean canUndo)
- : FW_CDragCommand(ev, frame, canUndo),
- fDrawPart(part),
- fDrawSelection(selection),
- fDraggedContent(NULL)
- {
- }
-
- //----------------------------------------------------------------------------------------
- // CDrawDragCommand destructor
- //----------------------------------------------------------------------------------------
-
- CDrawDragCommand::~CDrawDragCommand()
- {
- delete fDraggedContent; // Will call remove all if necessary
- fDraggedContent = NULL;
- }
-
- //----------------------------------------------------------------------------------------
- // CDrawDragCommand::SaveUndoState
- //----------------------------------------------------------------------------------------
-
- void CDrawDragCommand::SaveUndoState(Environment* ev) // Override
- {
- FW_ASSERT(fDraggedContent == NULL);
- fDraggedContent = FW_NEW(CDrawContent, (ev, fDrawSelection->GetDrawContent(ev)));
- }
-
- //----------------------------------------------------------------------------------------
- // CDrawDragCommand::UndoIt
- //----------------------------------------------------------------------------------------
-
- void CDrawDragCommand::UndoIt(Environment* ev) // Override
- {
- // ----- Add the dragged shapes back into the part
- CDrawContentShapeIterator ite(fDraggedContent);
- for (CBaseShape* shape = ite.First(); ite.IsNotComplete(); shape = ite.Next())
- {
- shape->RestoreShape(ev, fDrawPart);
- }
-
- // ----- Add the saved shapes to the selection
- fDrawSelection->SelectContent(ev, fDraggedContent);
-
- // ----- Invalidate
- GetPresentation(ev)->Invalidate(ev, fDrawSelection->GetUpdateShape());
- }
-
- //----------------------------------------------------------------------------------------
- // CDrawDragCommand::RedoIt
- //----------------------------------------------------------------------------------------
-
- void CDrawDragCommand::RedoIt(Environment* ev) // Override
- {
- // Select saved shapes
- fDrawSelection->SelectContent(ev, fDraggedContent);
-
- fDrawSelection->ClearSelection(ev); // clear them, again
- }
-
- //----------------------------------------------------------------------------------------
- // CDrawDragCommand::FreeUndoState
- //----------------------------------------------------------------------------------------
-
- void CDrawDragCommand::FreeUndoState(Environment *ev) // Override
- {
- // Delete the dragged shapes - they're gone forever
- CBaseShape* shape;
-
- //-- First check whether the shapes are promised
- CDrawContentShapeIterator ite(fDraggedContent);
- for (shape = ite.First(); ite.IsNotComplete(); shape = ite.Next())
- {
- shape->CheckPromise(ev, fDrawPart);
- }
-
- //-- Now delete them
- while ((shape = fDraggedContent->GetFirstShape()) != NULL)
- {
- fDraggedContent->RemoveShape(ev, shape);
- delete shape;
- }
- }
-
- //========================================================================================
- // class CDrawDropCommand
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // CDrawDropCommand constructor
- //----------------------------------------------------------------------------------------
- CDrawDropCommand::CDrawDropCommand(Environment* ev,
- CDrawPart* part,
- FW_CFrame* frame,
- ODDragItemIterator* dropInfo,
- ODFacet* facet,
- const FW_CPoint& windowPoint,
- FW_Boolean canUndo)
- : FW_CDropCommand(ev, frame, dropInfo, facet, windowPoint, canUndo),
- fDrawPart(part),
- fSavedLink(NULL),
- fDropDelta(FW_kZeroPoint),
- fDroppedContent(NULL),
- fDrawSelection((CDrawSelection*)GetPresentation(ev)->GetSelection(ev))
- {
- }
-
- //----------------------------------------------------------------------------------------
- // CDrawDropCommand destructor
- //----------------------------------------------------------------------------------------
-
- CDrawDropCommand::~CDrawDropCommand()
- {
- delete fDroppedContent;
- }
-
- //----------------------------------------------------------------------------------------
- // CDrawDropCommand::DoDrop
- //----------------------------------------------------------------------------------------
- FW_Boolean CDrawDropCommand::DoDrop(Environment* ev,
- ODStorageUnit* dropSU,
- const FW_CPoint& mouseDownOffset,
- const FW_CPoint& dropPoint,
- FW_Boolean isDropMove,
- short itemNumber)
- {
- if (itemNumber == 1)
- fDrawSelection->CloseSelection(ev);
-
- FW_Boolean result = FW_CDropCommand::DoDrop(ev, dropSU, mouseDownOffset, dropPoint, isDropMove, itemNumber);
-
- if (result)
- {
- fDrawPart->SetTool(ev, kSelectTool);
-
- fDrawSelection->CalcCache(ev);
-
- FW_CPoint newPosition(dropPoint.x - mouseDownOffset.x, dropPoint.y - mouseDownOffset.y);
-
- FW_CRect box;
- fDrawSelection->GetDragRect(box);
-
- fDrawSelection->OffsetSelection(ev, newPosition.x - box.left, newPosition.y - box.top);
-
- ODShape* updateShape = fDrawSelection->GetUpdateShape();
-
- // ----- Calculate clip -----
- CDrawFacetClipper facetClipper(ev, fDrawPart);
- facetClipper.Clip(ev, fDrawSelection->GetPresentation(ev), updateShape);
-
- // ----- Invalidate -----
- GetPresentation(ev)->Invalidate(ev, updateShape);
-
- // ----- If I am not the active part I should not have a selection -----
- if (!fDrawPart->HasSelectionFocus(ev))
- fDrawSelection->CloseSelection(ev);
- }
-
- return result;
- }
-
- //----------------------------------------------------------------------------------------
- // CDrawDropCommand::DoDroppedInSameFrame
- //----------------------------------------------------------------------------------------
-
- FW_Boolean CDrawDropCommand::DoDroppedInSameFrame(Environment* ev,
- ODStorageUnit* dropSU,
- const FW_CPoint& mouseDownOffset,
- const FW_CPoint& dropPoint)
- {
- FW_UNUSED(dropSU);
-
- FW_CPoint newPosition(dropPoint.x - mouseDownOffset.x, dropPoint.y - mouseDownOffset.y);
-
- FW_CRect box;
- fDrawSelection->GetDragRect(box);
-
- fDropDelta.Set(newPosition.x - box.left, newPosition.y - box.top);
-
- this->OffsetSelection(ev, fDropDelta);
-
- return TRUE;
- }
-
- //----------------------------------------------------------------------------------------
- // CDrawDropCommand::SaveRedoState
- //----------------------------------------------------------------------------------------
-
- void CDrawDropCommand::SaveRedoState(Environment* ev) // Override
- {
- if (fSavedLink == NULL) // if a link was created, don't need to save anything
- {
- // Save shapes that were just dropped
- FW_ASSERT(fDroppedContent == NULL);
- fDroppedContent = FW_NEW(CDrawContent, (ev, fDrawSelection->GetDrawContent(ev)));
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CDrawDropCommand::UndoIt
- //----------------------------------------------------------------------------------------
- void CDrawDropCommand::UndoIt(Environment* ev) // Override
- {
- if (fSavedLink)
- {
- GetDrawLinkManager(ev)->UndoPasteAs(ev, fSavedLink);
- }
- else if (this->IsDragMoveInSameFrame(ev)) // it's a drag-move
- {
- // select dropped shapes and move them back to the original position
- fDrawSelection->SelectContent(ev, fDroppedContent);
- this->OffsetSelection(ev, -fDropDelta);
- fDrawSelection->SelectionChanged(ev); // need more than this for redraw???
- }
- else
- {
- // select dropped shapes and remove them from the document
- fDrawSelection->SelectContent(ev, fDroppedContent);
- fDrawSelection->ClearSelection(ev);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CDrawDropCommand::RedoIt
- //----------------------------------------------------------------------------------------
- void CDrawDropCommand::RedoIt(Environment* ev) // Override
- {
- if (fSavedLink)
- {
- GetDrawLinkManager(ev)->RedoPasteAs(ev, fSavedLink);
- this->OffsetSelection(ev, -fDropDelta);
- }
- else if (this->IsDragMoveInSameFrame(ev)) // it's a drag-move
- {
- // select dropped shapes and move them to new position
- fDrawSelection->SelectContent(ev, fDroppedContent);
- this->OffsetSelection(ev, fDropDelta);
- }
- else // dropped shapes are copies
- {
- // add dropped shapes back into document
- this->RestoreDroppedShapes(ev);
- GetPresentation(ev)->Invalidate(ev, fDrawSelection->GetUpdateShape());
- }
- fDrawSelection->SelectionChanged(ev);
- }
-
- //----------------------------------------------------------------------------------------
- // CDrawDropCommand::RestoreDroppedShapes
- //----------------------------------------------------------------------------------------
- void CDrawDropCommand::RestoreDroppedShapes(Environment* ev)
- {
- // Add the dropped shapes back into the part
- CDrawContentShapeIterator ite(fDroppedContent);
- for (CBaseShape* shape = ite.First(); ite.IsNotComplete(); shape = ite.Next())
- {
- shape->RestoreShape(ev, fDrawPart);
- }
-
- // Add the saved shapes to the selection
- fDrawSelection->SelectContent(ev, fDroppedContent);
- }
-
- //----------------------------------------------------------------------------------------
- // CDrawDropCommand::OffsetSelection
- //----------------------------------------------------------------------------------------
-
- void CDrawDropCommand::OffsetSelection(Environment* ev, const FW_CPoint& delta)
- {
- if (delta != FW_kZeroPoint)
- {
- GetPresentation(ev)->Invalidate(ev, fDrawSelection->GetUpdateShape());
- fDrawSelection->OffsetSelection(ev, delta.x, delta.y);
- GetPresentation(ev)->Invalidate(ev, fDrawSelection->GetUpdateShape());
-
- // Adjust offsets of linked shapes
- CDrawContentShapeIterator it(fDrawSelection->GetDrawContent(ev));
- for (CBaseShape* shape = it.First(); it.IsNotComplete(); shape = it.Next())
- {
- if (shape->GetSubscribeLink() != NULL)
- {
- shape->GetSubscribeLink()->AdjustUpdateOffset(ev, delta);
- }
- }
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CDrawDropCommand::CommitUndone
- //----------------------------------------------------------------------------------------
-
- void CDrawDropCommand::CommitUndone(Environment* ev) // Override
- {
- if (fSavedLink == NULL)
- FW_CDropCommand::CommitUndone(ev);
- }
-
- //----------------------------------------------------------------------------------------
- // CDrawDropCommand::FreeRedoState
- //----------------------------------------------------------------------------------------
-
- void CDrawDropCommand::FreeRedoState(Environment* ev) // Override
- {
- //-- Delete the the dropped shapes
- CBaseShape* shape;
- while ((shape = fDroppedContent->GetFirstShape()) != NULL)
- {
- fDroppedContent->RemoveShape(ev, shape);
- delete shape;
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CDrawDropCommand::GetDrawLinkManager
- //----------------------------------------------------------------------------------------
-
- CDrawLinkManager* CDrawDropCommand::GetDrawLinkManager(Environment* ev) const
- {
- return (CDrawLinkManager*) fDrawPart->GetLinkManager(ev);
- }
-
- //----------------------------------------------------------------------------------------
- // CDrawDropCommand::DoDroppedPasteAs
- //----------------------------------------------------------------------------------------
-
- void CDrawDropCommand::DoDroppedPasteAs(Environment* ev,
- const FW_CPoint& mouseDownOffset,
- const FW_CPoint& dropPoint)
- {
- fDropDelta = dropPoint - mouseDownOffset;
- fSavedLink = (CDrawSubscribeLink*) this->GetNewLink(ev);
-
- if (fSavedLink)
- {
- // Save information about newly-created link
- fSavedLink->SetUpdateOffset(ev, fDropDelta);
-
- if (fDropDelta != FW_kZeroPoint)
- {
- // invalidate and clear the current selection
- GetPresentation(ev)->Invalidate(ev, fDrawSelection->GetUpdateShape());
- fDrawSelection->CloseSelection(ev); // empty the selection
-
- // select the dropped shapes
- CDrawLinkManager* linkMgr = GetDrawLinkManager(ev);
- linkMgr->SelectSubscribedShapes(ev, fSavedLink);
-
- // offset the dropped shapes
- fDrawSelection->OffsetSelection(ev, fDropDelta.x, fDropDelta.y);
- GetPresentation(ev)->Invalidate(ev, fDrawSelection->GetUpdateShape());
- }
- }
- else // no link created - must be Embed As
- {
- /* Code copied from DoDrop */
- fDrawPart->SetTool(ev, kSelectTool);
-
- fDrawSelection->CalcCache(ev);
-
- FW_CRect box;
- fDrawSelection->GetDragRect(box);
-
- fDrawSelection->OffsetSelection(ev, fDropDelta.x - box.left, fDropDelta.y - box.top);
-
- ODShape* updateShape = fDrawSelection->GetUpdateShape();
-
- // ----- Calculate clip -----
- CDrawFacetClipper facetClipper(ev, fDrawPart);
- facetClipper.Clip(ev, fDrawSelection->GetPresentation(ev), updateShape);
-
- // ----- Invalidate -----
- GetPresentation(ev)->Invalidate(ev, updateShape);
- }
- }
-